# -*- coding: utf-8 -*-
import scrapy
import re
from ..items import FangtianxiaItem


class FangSpider(scrapy.Spider):
    name = 'fang'
    # allowed_domains = ['xxx']
    start_urls = ['https://zu.fang.com/house-a00/c21000-d22000/']

    def parse(self, response):
        all_dl = response.xpath(
            "/html/body/div[@class='wrap']/div[@id='houselistbody']/div[@id='listBox']/div[@class='houseList']/dl/dd")
        for dl in all_dl:
            url = "https://zu.fang.com" + str(dl.xpath("./p/a/@href").extract()[0])
            yield scrapy.Request(url=url, callback=self.parse_content, meta={"url": url})

    def parse_content(self, response):
        item = FangtianxiaItem()
        item['title'] = response.xpath("//h1[@class='title']/text()").extract()[0]  # 标题
        item['fangshi'] = response.xpath(
            "//div[@class='tab-cont-right']/div[@class='tr-line clearfix'][1]/div[@class='trl-item1 w146']/div[@class='tt']/text()").extract()[
            0]  # 方式
        item['huxing'] = response.xpath(
            "//div[@class='tr-line clearfix'][1]/div[@class='trl-item1 w182']/div[@class='tt']/text()").extract()[
            0]  # 户型
        item['mianji'] = response.xpath(
            "//div[@class='tr-line clearfix'][1]/div[@class='trl-item1 w132']/div[@class='tt']/text()").extract()[
            0]  # 面积
        chaoxiang_data = \
            response.xpath(
                "//div[@class='tr-line clearfix'][2]/div[@class='trl-item1 w146']/div[@class='tt']").extract()[
                0]  # 朝向
        item['chaoxiang'] = re.findall('\>.*?\<', chaoxiang_data)[0][1:-1]
        item['jiage'] = response.xpath("//div[@class='tr-line clearfix zf_new_title']/div/i/text()").extract()[0]  # 价格
        item['tu_url'] = response.xpath(
            "/html/body/div[@class='wid1200 clearfix'][2]/div[@class='tab-cont clearfix']/div[@class='tab-cont-left']/div[@id='imgSlide1']/a[@id='agantzfxq_C02_06']/div[@class='little-img']/ul[@class='litImg']/li[1]/img/@src").extract()[
            0]  # 图片url
        item['xiaoqu'] = response.xpath(
            "/html/body/div[@class='wid1200 clearfix'][2]/div[@class='tab-cont clearfix']/div[@class='tab-cont-right']/div[@class='tr-line']/div[@class='trl-item2 clearfix'][1]/div[@class='rcont']/a/text()").extract()[
            0]  # 小区
        item['dizhi'] = response.xpath(
            "/html/body/div[@class='wid1200 clearfix'][2]/div[@class='tab-cont clearfix']/div[@class='tab-cont-right']/div[@class='tr-line']/div[@class='trl-item2 clearfix'][2]/div[@class='rcont']/a/text()").extract()[
            0]  # 地址
        item['gengxin'] = response.xpath(
            "/html/body/div[@class='wid1200 clearfix'][2]/div[@class='tab-cont clearfix']/p[@class='gray9 fybh-zf']/span[2]/text()").extract()[
            0]  # 时间
        print(item['title'])
        yield item
